#
# @lc app=leetcode.cn id=1005 lang=python
#
# [1005] K 次取反后最大化的数组和
#

# @lc code=start
class Solution(object):
    def largestSumAfterKNegations(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        sums = 0
        nums.sort(key=lambda x: abs(x), reverse=True)
        for i in range(len(nums)):
            if nums[i] < 0 and k>0 :
                nums[i] = - nums[i]
                k -= 1
            sums += nums[i]
        if k%2 > 0:
            sums -= 2*nums[-1]

        return sums
        
# @lc code=end

